<script setup lang='ts'>
  import { onMounted, reactive, ref } from 'vue';
  import { accountApi } from '@/api/controller';
  import { Toastloading } from '@/utils/public';
  import { getPlayEndTime, getDateArray } from '@/utils'
  import xmImage from '@/components/xmImage/index.vue';
  import Pagination from "@/components/Pagination/index.vue";
  import addDiamondForm from './addDiamondForm.vue';
  const addDiamondFormRef = ref()
  const action = reactive({
    total:0,
    list:[] as any[],
    dateArr:getDateArray('ymd') as Array<number>,
  })
  const typeOptions = [
    { label: "扣款", value: 1 },
    { label: "赠送", value: 2 },
  ]
  const handleForm = (type:any = -1)=>{
    if(type < 0) return console.error('缺少type')
    addDiamondFormRef.value.openDialog({
      type
    })
  }
  
  const listQuery = reactive({
    page:1,
    limit:20,
    uuid:'',
    nickname:'',
    type:''
  })
  const resetQuery = ()=>{
    Object.assign(listQuery,{
      page:1,
      limit:20,
      uuid:'',
      nickname:'',
      type:''
    })
    action.dateArr = getDateArray('ymd') as Array<number>
    getList()
  }
  const handleSerach = ()=>{
    listQuery.page = 1
    getList()
  }
  const getList = async()=> {
    const loading = Toastloading()
    const timeObi = getPlayEndTime(action.dateArr)
    const res = await accountApi.getfundList({
      begin:timeObi.playTime,
      end:timeObi.endTime,
      ...listQuery
    })
    loading.close()
    const { data=[], total=0 } = res.data
    action.total = total
    action.list = data
  }
  onMounted(()=>{
    getList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">用户UUID：</div>
          <el-input v-model="listQuery.uuid" placeholder="请输入UUID"/>
        </div>
        <div class="labelItem">
          <div class="label">用户昵称：</div>
          <el-input v-model="listQuery.nickname" placeholder="请输入用户昵称"/>
        </div>
        <div class="labelItem">
          <div class="label">日期：</div>
          <el-date-picker
            v-model="action.dateArr"
            type="daterange"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          />
        </div>
        <div class="labelItem">
          <div class="label">查询类型：</div>
          <el-select v-model="listQuery.type" placeholder="请选择查询类型">
            <el-option
            v-for="item in typeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value" />
          </el-select>
        </div>
        <div class="labelItem">
          <el-button type="primary" @click="handleSerach">查询</el-button>
          <el-button type="primary" @click="resetQuery">重置</el-button>
          <el-button type="danger" @click="handleForm(1)">扣款</el-button>
          <el-button type="primary" @click="handleForm(2)">赠送</el-button>
        </div>
      </div>
    </div>
    <el-table class="xm-main xm-TableCenter" :data="action.list" border>
      <el-table-column min-width="80" prop="id" label="ID" />
      <el-table-column min-width="80" prop="uuid" label="UUID" />
      <el-table-column min-width="120" show-overflow-tooltip prop="nickname" label="用户昵称" />
      <el-table-column min-width="80" prop="change_diamond" label="变动金额" />
      <el-table-column min-width="200" show-overflow-tooltip prop="desc" label="说明" />
      <el-table-column min-width="120" label="凭证">
        <template #default="scope">
          <xmImage :src="scope.row.img" :list="[scope.row.img]" />
        </template>
      </el-table-column>
      <el-table-column min-width="200" show-overflow-tooltip prop="sys_notice" label="系统通知" />
      <el-table-column width="80" label="类型">
        <template #default=scope>
          <el-tag>
            {{ typeOptions.find(res=>res.value == scope.row.type)?.label || scope.row.type }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column width="180" prop="created_at" label="创建时间" />
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
    <addDiamondForm ref="addDiamondFormRef" @update-list="getList" />
  </div>
</template>